using System;
using Microsoft.Win32;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace BreezeConfig
{
	/// <summary>
	/// Main window
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.TextBox idBox;
		private System.Windows.Forms.TextBox hostnameBox;
		private System.Windows.Forms.TextBox usernameBox;
		private System.Windows.Forms.TextBox passwordBox;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Button pathBrowse;
		private System.Windows.Forms.Button saveButton;
		private System.Windows.Forms.Button newButton;
		private System.Windows.Forms.Button deleteButton;
		private System.Windows.Forms.Button aboutButton;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.ComboBox authtypeBox;
		private System.Windows.Forms.TextBox keypathBox;
		private System.Windows.Forms.TextBox pathBox;
		private System.Windows.Forms.Button advancedButton;
		private System.Windows.Forms.Label infoboxLabel;
		private System.Windows.Forms.Button listUpButton;
		private System.Windows.Forms.Button listDownButton;
		private Hashtable listHash = new Hashtable();
		private System.Windows.Forms.OpenFileDialog keypathfileDialog;
		private string configData = "";

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.idBox = new System.Windows.Forms.TextBox();
			this.hostnameBox = new System.Windows.Forms.TextBox();
			this.usernameBox = new System.Windows.Forms.TextBox();
			this.passwordBox = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.saveButton = new System.Windows.Forms.Button();
			this.keypathBox = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.newButton = new System.Windows.Forms.Button();
			this.deleteButton = new System.Windows.Forms.Button();
			this.pathBox = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.pathBrowse = new System.Windows.Forms.Button();
			this.aboutButton = new System.Windows.Forms.Button();
			this.advancedButton = new System.Windows.Forms.Button();
			this.authtypeBox = new System.Windows.Forms.ComboBox();
			this.label7 = new System.Windows.Forms.Label();
			this.infoboxLabel = new System.Windows.Forms.Label();
			this.listUpButton = new System.Windows.Forms.Button();
			this.listDownButton = new System.Windows.Forms.Button();
			this.keypathfileDialog = new System.Windows.Forms.OpenFileDialog();
			this.SuspendLayout();
			// 
			// listBox1
			// 
			this.listBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.listBox1.Location = new System.Drawing.Point(8, 8);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(207, 197);
			this.listBox1.TabIndex = 0;
			this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
			// 
			// idBox
			// 
			this.idBox.Location = new System.Drawing.Point(258, 177);
			this.idBox.Name = "idBox";
			this.idBox.Size = new System.Drawing.Size(346, 20);
			this.idBox.TabIndex = 5;
			this.idBox.Text = "";
			// 
			// hostnameBox
			// 
			this.hostnameBox.Location = new System.Drawing.Point(259, 31);
			this.hostnameBox.Name = "hostnameBox";
			this.hostnameBox.Size = new System.Drawing.Size(157, 20);
			this.hostnameBox.TabIndex = 1;
			this.hostnameBox.Text = "";
			this.hostnameBox.LostFocus += new System.EventHandler(this.hostnameBox_TextChanged);
			// 
			// usernameBox
			// 
			this.usernameBox.Location = new System.Drawing.Point(446, 31);
			this.usernameBox.Name = "usernameBox";
			this.usernameBox.Size = new System.Drawing.Size(157, 20);
			this.usernameBox.TabIndex = 2;
			this.usernameBox.Text = "";
			this.usernameBox.LostFocus += new System.EventHandler(this.usernameBox_TextChanged);
			// 
			// passwordBox
			// 
			this.passwordBox.Location = new System.Drawing.Point(446, 75);
			this.passwordBox.Name = "passwordBox";
			this.passwordBox.PasswordChar = '*';
			this.passwordBox.Size = new System.Drawing.Size(157, 20);
			this.passwordBox.TabIndex = 3;
			this.passwordBox.Text = "";
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(257, 161);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(38, 14);
			this.label1.TabIndex = 0;
			this.label1.Text = "name:";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(258, 14);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(88, 13);
			this.label2.TabIndex = 0;
			this.label2.Text = "hostname or ip:";
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Location = new System.Drawing.Point(446, 14);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(62, 16);
			this.label3.TabIndex = 0;
			this.label3.Text = "username:";
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label4.Location = new System.Drawing.Point(446, 59);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(64, 15);
			this.label4.TabIndex = 0;
			this.label4.Text = "password:";
			// 
			// saveButton
			// 
			this.saveButton.Location = new System.Drawing.Point(515, 207);
			this.saveButton.Name = "saveButton";
			this.saveButton.Size = new System.Drawing.Size(90, 23);
			this.saveButton.TabIndex = 6;
			this.saveButton.Text = "Save";
			this.saveButton.Click += new System.EventHandler(this.saveButton_Click);
			// 
			// keypathBox
			// 
			this.keypathBox.Location = new System.Drawing.Point(446, 127);
			this.keypathBox.Name = "keypathBox";
			this.keypathBox.Size = new System.Drawing.Size(157, 20);
			this.keypathBox.TabIndex = 9;
			this.keypathBox.Text = "";
			// 
			// label5
			// 
			this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label5.Location = new System.Drawing.Point(446, 109);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(88, 14);
			this.label5.TabIndex = 0;
			this.label5.Text = "path to ssh key:";
			// 
			// newButton
			// 
			this.newButton.Location = new System.Drawing.Point(405, 207);
			this.newButton.Name = "newButton";
			this.newButton.Size = new System.Drawing.Size(90, 23);
			this.newButton.TabIndex = 11;
			this.newButton.Text = "New";
			this.newButton.Click += new System.EventHandler(this.newButton_Click);
			// 
			// deleteButton
			// 
			this.deleteButton.Location = new System.Drawing.Point(116, 212);
			this.deleteButton.Name = "deleteButton";
			this.deleteButton.Size = new System.Drawing.Size(90, 23);
			this.deleteButton.TabIndex = 12;
			this.deleteButton.Text = "Delete";
			this.deleteButton.Click += new System.EventHandler(this.deleteButton_Click);
			// 
			// pathBox
			// 
			this.pathBox.Location = new System.Drawing.Point(259, 75);
			this.pathBox.Name = "pathBox";
			this.pathBox.Size = new System.Drawing.Size(157, 20);
			this.pathBox.TabIndex = 4;
			this.pathBox.Text = "";
			this.pathBox.LostFocus += new System.EventHandler(this.pathBox_TextChanged);
			// 
			// label6
			// 
			this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label6.Location = new System.Drawing.Point(258, 59);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(72, 14);
			this.label6.TabIndex = 0;
			this.label6.Text = "remote path:";
			// 
			// pathBrowse
			// 
			this.pathBrowse.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.pathBrowse.Location = new System.Drawing.Point(547, 152);
			this.pathBrowse.Name = "pathBrowse";
			this.pathBrowse.Size = new System.Drawing.Size(56, 16);
			this.pathBrowse.TabIndex = 10;
			this.pathBrowse.Text = "browse...";
			this.pathBrowse.Click += new System.EventHandler(this.pathBrowse_Click);
			// 
			// aboutButton
			// 
			this.aboutButton.Location = new System.Drawing.Point(16, 245);
			this.aboutButton.Name = "aboutButton";
			this.aboutButton.Size = new System.Drawing.Size(90, 23);
			this.aboutButton.TabIndex = 13;
			this.aboutButton.Text = "About..";
			this.aboutButton.Click += new System.EventHandler(this.aboutButton_Click);
			// 
			// advancedButton
			// 
			this.advancedButton.Location = new System.Drawing.Point(116, 245);
			this.advancedButton.Name = "advancedButton";
			this.advancedButton.Size = new System.Drawing.Size(90, 23);
			this.advancedButton.TabIndex = 14;
			this.advancedButton.Text = "Advanced";
			this.advancedButton.Click += new System.EventHandler(this.advancedButton_Click);
			// 
			// authtypeBox
			// 
			this.authtypeBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.authtypeBox.Items.AddRange(new object[] {
															 "password",
															 "private key",
															 "user prompt"});
			this.authtypeBox.Location = new System.Drawing.Point(259, 127);
			this.authtypeBox.Name = "authtypeBox";
			this.authtypeBox.Size = new System.Drawing.Size(157, 21);
			this.authtypeBox.TabIndex = 8;
			this.authtypeBox.SelectedIndexChanged += new System.EventHandler(this.authtypeBox_SelectedIndexChanged);
			// 
			// label7
			// 
			this.label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label7.Location = new System.Drawing.Point(258, 110);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(116, 15);
			this.label7.TabIndex = 16;
			this.label7.Text = "authorization type:";
			// 
			// infoboxLabel
			// 
			this.infoboxLabel.Location = new System.Drawing.Point(253, 242);
			this.infoboxLabel.Name = "infoboxLabel";
			this.infoboxLabel.Size = new System.Drawing.Size(358, 13);
			this.infoboxLabel.TabIndex = 22;
			this.infoboxLabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
			// 
			// listUpButton
			// 
			this.listUpButton.Location = new System.Drawing.Point(17, 212);
			this.listUpButton.Name = "listUpButton";
			this.listUpButton.Size = new System.Drawing.Size(39, 23);
			this.listUpButton.TabIndex = 23;
			this.listUpButton.Text = "<";
			this.listUpButton.Click += new System.EventHandler(this.listUpButton_Click);
			// 
			// listDownButton
			// 
			this.listDownButton.Location = new System.Drawing.Point(65, 212);
			this.listDownButton.Name = "listDownButton";
			this.listDownButton.Size = new System.Drawing.Size(40, 23);
			this.listDownButton.TabIndex = 24;
			this.listDownButton.Text = ">";
			this.listDownButton.Click += new System.EventHandler(this.listDownButton_Click);
			// 
			// keypathfileDialog
			// 
			this.keypathfileDialog.AddExtension = false;
			this.keypathfileDialog.Filter = "Putty Public Key (*.ppk)|*.ppk|All Files (*.*)|*.*";
			this.keypathfileDialog.Title = "Browsing...";
			this.keypathfileDialog.FileOk += new System.ComponentModel.CancelEventHandler(this.keypathfileDialog_FileOk);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(631, 279);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.listDownButton,
																		  this.listUpButton,
																		  this.infoboxLabel,
																		  this.label7,
																		  this.authtypeBox,
																		  this.advancedButton,
																		  this.aboutButton,
																		  this.pathBrowse,
																		  this.label6,
																		  this.pathBox,
																		  this.deleteButton,
																		  this.newButton,
																		  this.label5,
																		  this.keypathBox,
																		  this.saveButton,
																		  this.label4,
																		  this.label3,
																		  this.label2,
																		  this.label1,
																		  this.passwordBox,
																		  this.usernameBox,
																		  this.hostnameBox,
																		  this.idBox,
																		  this.listBox1});
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "Form1";
			this.Text = "BreezeSCP Config";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
	
		private void Form1_Load(object sender, System.EventArgs e)
		{
			string keyStr;

			RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\BreezeWorkz\\BreezeSCP");

			if ( key == null )
			{
				MessageBox.Show("Can't locate configuration path.\n\nPlease try reinstalling.\n\n", "Error", MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
				System.Environment.Exit(1);
			}

			keyStr = key.GetValue("ConfigPath").ToString();
			this.configData = keyStr;
			key.Close();

			if ( keyStr == null )
			{
				MessageBox.Show("Can't locate configuration path.\n\nPlease try reinstalling.\n\n", "Error", MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
				System.Environment.Exit(1);
			}

			this.loadConfig();
		}

		private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if ( this.listBox1.SelectedItem == null )
				return;

			this.scrub();

			string tStr;
			tStr = "\t";
			char[] delim = tStr.ToCharArray();
			tStr = "";
			string [] tArray;
			int tCount = 0;

			foreach ( DictionaryEntry tDe in this.listHash )
			{
				if ( tDe.Key.Equals(this.listBox1.SelectedItem) )
				{
					tStr = tDe.Value.ToString();
					break;
				}
			}

			if ( tStr.ToString() == "" )
				return;

			tArray = tStr.Split(delim, 4);

			tCount = 0;

			foreach (string tStr2 in tArray)
			{
				if ( tCount == 0 )
				{
					this.idBox.Text = tStr2.ToString();
				}
				else if ( tCount == 1 )
				{
					this.usernameBox.Text = tStr2.Substring(0, tStr2.IndexOf("@"));
					tStr = tStr2.Substring(0, tStr2.IndexOf(":"));
					this.hostnameBox.Text = tStr.Substring(tStr2.IndexOf("@")+1);
					this.pathBox.Text = tStr2.Substring(tStr2.IndexOf(":")+1);
				}
				else if ( tCount == 2 )
				{
					if ( tStr2.ToString() == "1" )
					{
						this.authtypeBox.Text = "password";
					}
					else if ( tStr2.ToString() == "2" )
					{
						this.authtypeBox.Text = "private key";
					}
					else if ( tStr2.ToString() == "3" )
					{
						this.authtypeBox.Text = "user prompt";
					}
				}
				else if ( tCount == 3 )
				{
					if ( this.authtypeBox.Text == "password" )
					{
						this.passwordBox.Text = tStr2.ToString();
					}

					if ( this.authtypeBox.Text == "private key" )
					{
						this.keypathBox.Text = tStr2.ToString();
					}
				}

				tCount++;
			}
		}

		private void newButton_Click(object sender, System.EventArgs e)
		{
			this.scrub();
			this.hostnameBox.Focus();

			if ( this.listBox1.SelectedIndex != -1 )
				this.listBox1.SetSelected(this.listBox1.SelectedIndex, false);
		}

		private void deleteButton_Click(object sender, System.EventArgs e)
		{
			if ( this.listBox1.SelectedItem == null )
				return;

			int sIndex = this.listBox1.SelectedIndex;

			this.listHash.Remove(this.listBox1.SelectedItem);
			this.listBox1.Items.Remove(this.listBox1.SelectedItem);

			this.writedown();
			this.listBox1.ClearSelected();
			this.scrub();
		}

		private void authtypeBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if ( this.authtypeBox.SelectedItem.ToString() == "password" )
			{
				this.label5.Enabled = false;
				this.passwordBox.Enabled = true;
				this.pathBrowse.Enabled = false;
				this.label4.Enabled = true;
				this.keypathBox.Enabled = false;
			}
			else if ( this.authtypeBox.SelectedItem.ToString() == "private key" )
			{
				this.keypathBox.Enabled = true;
				this.label5.Enabled = true;
				this.pathBrowse.Enabled = true;
				this.label4.Enabled = false;
				this.passwordBox.Enabled = false;
			}
			else if ( this.authtypeBox.SelectedItem.ToString() == "user prompt" )
			{
				this.label5.Enabled = false;
				this.keypathBox.Enabled = false;
				this.pathBrowse.Enabled = false;
				this.label5.Enabled = false;
				this.passwordBox.Enabled = false;				
			}
		}

		private void saveButton_Click(object sender, System.EventArgs e)
		{
			this.infoboxLabel.ForeColor = System.Drawing.Color.Tomato;

			if ( this.hostnameBox.Text == "" )
			{
				this.infoboxLabel.Text = "please enter a hostname or ip";
				this.hostnameBox.Focus();
				return;
			}

			if ( this.usernameBox.Text == "" )
			{
				this.infoboxLabel.Text = "please enter a username";
				this.usernameBox.Focus();
				return;
			}

			if ( this.pathBox.Text == "" )
			{
				this.infoboxLabel.Text = "please enter a remote path";
				this.pathBox.Focus();
				return;
			}

			if ( (this.authtypeBox.Text == "") || ( (this.authtypeBox.Text != "password") &&
				 (this.authtypeBox.Text != "private key") && (this.authtypeBox.Text != "user prompt") ) )
			{
				this.infoboxLabel.Text = "please select an authentication type";
				this.authtypeBox.Focus();
				return;
			}

			if ( ( this.authtypeBox.Text == "private key" ) && ( this.keypathBox.Text == "" ) )
			{
				this.infoboxLabel.Text = "please enter the path to your private key";
				this.keypathBox.Focus();
				return;
			}

			if ( ( this.authtypeBox.Text == "password" ) && ( this.passwordBox.Text == "" ) )
			{
				this.infoboxLabel.Text = "please enter a password";
				this.passwordBox.Focus();
				return;
			}

			//if ( regexbleh )
			if ( this.idBox.Text == "" )
			{
				this.infoboxLabel.Text = "please enter a name for this configuration";
				this.idBox.Focus();
				return;
			}
			else
			{
				//name
				string tStr = "";
				tStr += this.idBox.Text.ToString();
				tStr += "\t";

				//token
				tStr += this.usernameBox.Text.ToString();
				tStr += "@";
				tStr += this.hostnameBox.Text.ToString();
				tStr += ":";
				tStr += this.pathBox.Text.ToString();
				tStr += "\t";

				//authtype & auth
				if ( this.authtypeBox.Text.ToString() == "password" )
				{
					tStr += "1";
					tStr += "\t";
					tStr += this.passwordBox.Text.ToString();
				}
				else if ( this.authtypeBox.Text.ToString() == "private key" )
				{
					tStr += "2";
					tStr += "\t";
					tStr += this.keypathBox.Text.ToString();
				}
				else if ( this.authtypeBox.Text.ToString() == "user prompt" )
				{
					tStr += "3";
					tStr += "\t";
					tStr += "null";
				}

				this.infoboxLabel.Text = tStr.ToString();

				if ( this.listHash.ContainsKey(this.idBox.Text) )
				{
					this.listHash.Remove(this.idBox.Text);
				}
				else
				{
					this.listBox1.Items.Add(this.idBox.Text.ToString());
				}

				this.listHash.Add(this.idBox.Text.ToString(), tStr.ToString());

				this.writedown();
				this.listBox1.ClearSelected();
				this.scrub();
				this.hostnameBox.Focus();

				this.infoboxLabel.ForeColor = System.Drawing.Color.Green;
				this.infoboxLabel.Text = "configuration saved.";

				return;
			}
		}

		private void scrub()
		{
			this.authtypeBox.Text = "password";
			this.pathBox.Text = "";
			this.keypathBox.Text = "";
			this.passwordBox.Text = "";
			this.usernameBox.Text = "";
			this.hostnameBox.Text = "";
			this.idBox.Text = "";
			this.infoboxLabel.Text = "";
		}

		private void hostnameBox_TextChanged(object sender, System.EventArgs e)
		{
			string tStr = "";

			if ( ( this.usernameBox.Text != "" ) &&  ( this.hostnameBox.Text != "" ) &&
					( this.pathBox.Text != "" ) )
			{
				tStr += this.usernameBox.Text.ToString();
				tStr += "@";
				tStr += this.hostnameBox.Text.ToString();
				tStr += ":";
				tStr += this.pathBox.Text.ToString();

				this.idBox.Text = tStr;
			}
		}

		private void usernameBox_TextChanged(object sender, System.EventArgs e)
		{
			string tStr = "";

			if ( ( this.usernameBox.Text != "" ) &&  ( this.hostnameBox.Text != "" ) &&
				( this.pathBox.Text != "" ) )
			{
				tStr += this.usernameBox.Text.ToString();
				tStr += "@";
				tStr += this.hostnameBox.Text.ToString();
				tStr += ":";
				tStr += this.pathBox.Text.ToString();

				this.idBox.Text = tStr;
			}
		}

		private void pathBox_TextChanged(object sender, System.EventArgs e)
		{
			string tStr = "";

			if ( ( this.usernameBox.Text != "" ) &&  ( this.hostnameBox.Text != "" ) &&
				( this.pathBox.Text != "" ) )
			{
				tStr += this.usernameBox.Text.ToString();
				tStr += "@";
				tStr += this.hostnameBox.Text.ToString();
				tStr += ":";
				tStr += this.pathBox.Text.ToString();

				this.idBox.Text = tStr;
			}
		}

		private void listUpButton_Click(object sender, System.EventArgs e)
		{
			if ( ( this.listBox1.SelectedIndex  < 1 ) || ( this.listBox1.SelectedItem == null ) )
				return;

			int tIndex = this.listBox1.SelectedIndex;
			object tObj = this.listBox1.SelectedItem;

			this.listBox1.Items.Remove(this.listBox1.SelectedItem);
			this.listBox1.Items.Insert(tIndex-1, tObj);
			this.listBox1.SetSelected(tIndex-1, true);

			this.writedown();
		}

		private void listDownButton_Click(object sender, System.EventArgs e)
		{
			if ( ( ( ( this.listBox1.SelectedIndex ) +1 ) == this.listBox1.Items.Count ) ||
				( this.listBox1.SelectedItem == null ) )
				return;

			int tIndex = this.listBox1.SelectedIndex;
			object tObj = this.listBox1.SelectedItem;

			this.listBox1.Items.Remove(this.listBox1.SelectedItem);
			this.listBox1.Items.Insert(tIndex+1, tObj);
			this.listBox1.SetSelected(tIndex+1, true);

			this.writedown();
		}

		private void pathBrowse_Click(object sender, System.EventArgs e)
		{
			this.keypathfileDialog.ShowDialog();	
		}

		private void keypathfileDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if ( this.keypathfileDialog.FileName != null )
			{
				this.keypathBox.Text = this.keypathfileDialog.FileName.ToString();
				this.keypathBox.Focus();
			}
		}

		private void advancedButton_Click(object sender, System.EventArgs e)
		{
			Form2 popup = new Form2();
			popup.passKey = new Form2.PassKey(getKey);
			popup.ShowDialog();
		}

		private void aboutButton_Click(object sender, System.EventArgs e)
		{
			Form3 popup = new Form3();
			popup.ShowDialog();
		}

		private void writedown()
		{
			int i;
			int sIndex = -1;
			FileStream configFile;
			StreamWriter fStream;

			if ( this.listBox1.SelectedIndex != -1 )
				sIndex = this.listBox1.SelectedIndex;

			try
			{
				configFile = new FileStream(this.configData.ToString(), FileMode.Truncate, FileAccess.Write);
				fStream = new StreamWriter(configFile);

				this.listBox1.BeginUpdate();
				for (i = 0; i < this.listBox1.Items.Count; i++)
				{
					this.listBox1.SetSelected(i, true);

					foreach ( DictionaryEntry tDe in this.listHash )
					{
						if ( tDe.Key.Equals(this.listBox1.SelectedItem) )
						{
							fStream.WriteLine(tDe.Value.ToString());
							break;
						}
					}
				}

				if ( sIndex > 0 )
					this.listBox1.SetSelected(sIndex, true);

				if ( ( this.listBox1.Items.Count == 1 ) || ( sIndex == 0 ) )
					this.listBox1.SetSelected(0, true);

				this.listBox1.EndUpdate();

				fStream.Close();
				configFile.Close();
			}
			catch
			{
				MessageBox.Show("Can't write configuration data to disk.\n\nPlease try reinstalling.\n\n", "Error", MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
				System.Environment.Exit(1);
			}
		}

		private void getKey(string s)
		{
			if ( s != this.configData )
			{
				this.configData = s.ToString();
				this.loadConfig();
			}
		}

		private void loadConfig()
		{
			string tStr, tStr2;
			FileStream configFile;
			StreamReader fStream;

			if ( this.configData == null )
			{
				MessageBox.Show("Configuration path isn't set.\n", "Error", MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
				return;
			}

			this.listHash.Clear();
			this.listBox1.Items.Clear();

			try
			{
				configFile = new FileStream(this.configData.ToString(), FileMode.OpenOrCreate, FileAccess.Read);
				fStream = new StreamReader(configFile);

				this.listBox1.BeginUpdate();

				while ( ( tStr = fStream.ReadLine() ) != null )
				{
					if ( tStr.ToString() == "" )
						continue;
					
					int tabIndex = tStr.IndexOf("\t");
					if ( tabIndex < 1 )
						continue;

					tStr2 = tStr.Substring(0,tabIndex);
					this.listBox1.Items.Add(tStr2.ToString());
					this.listHash.Add(tStr2.ToString(), tStr.ToString());
				}

				this.listBox1.EndUpdate();

				fStream.Close();
				configFile.Close();
			}
			catch
			{
				MessageBox.Show("Can't read configuration data.\n\n", "Error", MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
			}

			this.listBox1.ClearSelected();
			this.scrub();
		}
	}
}
